﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.WebControls;
using NetDon.eBoss.Data.CollectionClasses;
using NetDon.eBoss.Data.EntityClasses;
using NetDon.Utilities;
using NetDon.eBoss.Data.HelperClasses;
using NetDon.eBoss.Portal.Common;
using NetDon.eBoss.Portal.HtmlTool;

namespace NetDon.OA.Portal.HtmlTool
{
    public partial class HtmlToolAccountEdit : NetDonPage
    {
        #region Properties
        protected int Id
        {
            get
            {
                return Parameters.GetInt32("Id");
            }
        }

        protected int AccountId
        {
            get
            {
                if (ViewState["AccountId"] == null) return 0;
                return Int32.Parse(ViewState["AccountId"].ToString());
            }
            set { ViewState["AccountId"] = value; }
        }


        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {

                CancelButton = btnReturn;
                BindDropDownLists();
                if (Id > 0)
                {
                    AccountId = Id;
                    var account = new HtmltoolaccountEntity();
                    account.FetchUsingPK(Id);
                    panelCustomInfo.DataSource = account;
                    panelCustomInfo.DataBind();

                    if (account.Sitecode.HasValue)
                        ddleBaysite.SelectedValue = EnumUtilities.Parse<eBaySites>(account.Sitecode.ToString()).ToString();
                    if (account.From.HasValue)
                        ddlAccountFrom.SelectedValue = account.From.ToString();


                }
                //bind tempalte
                var service = new HtmlToolAccountService();
                this.gridCustomerInfoList.DataSource = service.SearchHtmlToolAccountTemplate(Id);
                this.gridCustomerInfoList.DataBind();


            }
        }



        private void BindDropDownLists()
        {
            this.ddlAccountFrom.InsertEmptyItem(ListEmptyType.Select);
        }




        private bool save()
        {
            HtmltoolaccountEntity entity = null;
            if (Id > 0)
            {
                entity = new HtmltoolaccountEntity(Id);

            }
            else
            {
                entity = new HtmltoolaccountEntity();
            }


            var trans = new Transaction(IsolationLevel.ReadCommitted, "saveHtmltoolaccountEntity");
            try
            {
                if (ckbIsDefault.Checked)
                {
                    //set other to non-default is this is default
                    var accounts = new HtmltoolaccountCollection();
                    if (Id > 0)
                    {
                        accounts.GetMulti(HtmltoolaccountFields.Id != Id);
                    }
                    else
                    {
                        accounts.GetMulti(null);
                    }

                    foreach (var account in accounts)
                    {
                        account.Isdefault = 0;
                        account.Save();
                        trans.Add(account);
                    }
                }
                entity.Accountname = txtAccountname.Text;
                entity.Sitecode = (int)EnumUtilities.Parse<eBaySites>(ddleBaysite.SelectedValue);
                entity.From = ddlAccountFrom.SelectedValue.ToInt32();
                entity.Isdefault = (short)(ckbIsDefault.Checked ? 1 : 0);
                trans.Add(entity);
                entity.Save();

                foreach (GridViewRow detailRow in gridCustomerInfoList.Rows)
                {

                    var id = detailRow.Attributes["Id"].ToString().ToInt32();
                    var categoryId = detailRow.Attributes["CategoryId"].ToString().ToInt32();
                    var dllTemplate = (detailRow.FindControl("ddlTemplate") as DropDownList);
                    var htmltoolAccountTemplate = new HtmltoolaccounttemplateEntity();
                    if (id > 0)
                    {
                        htmltoolAccountTemplate = new HtmltoolaccounttemplateEntity(id.Value);

                    }
                    htmltoolAccountTemplate.Content = dllTemplate.SelectedValue.ToInt32();
                    htmltoolAccountTemplate.Categoryruleid = categoryId;
                    htmltoolAccountTemplate.Accountid = entity.Id;
                    trans.Add(htmltoolAccountTemplate);
                    htmltoolAccountTemplate.Save();
                }

                AccountId = Id;


                trans.Commit();




            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }


            return true;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (save())
            {
                Success("保存成功!");
                CloseWindow(true);
            }



        }


        protected void btnSaveTemplate_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(ddlAccountFrom.SelectedValue))
                {
                    var sourceAccount = ddlAccountFrom.SelectedValue.ToInt32().Value;
                    var service = new HtmlToolService();
                    service.CloneAccounts(sourceAccount, AccountId);
                }

                Success("复制模版成功!");
                CloseWindow(true);
            }
            catch (Exception)
            {
                
                throw;
            }
;
        }
        

        protected void gridCustomerInfoList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var id = e.Row.Attributes["Id"] = (e.Row.DataItem as dynamic).Id.ToString();
                var categoryid = e.Row.Attributes["CategoryId"] = (e.Row.DataItem as dynamic).CategoryId.ToString();
                var ddlTemplate = (e.Row.FindControl("ddlTemplate") as DropDownList);

                ddlTemplate.InsertEmptyItem(ListEmptyType.Select);

                ddlTemplate.SelectedValue = (e.Row.DataItem as dynamic).Content.ToString();
            }
        }



    }

}